home *** CD-ROM | disk | FTP | other *** search
- Path: in2.uu.net!zdc!szdc!news
- From: braz@ime.usp.br (Rodrigo de Salvo Braz)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ meta-problem
- Date: Mon, 15 Apr 1996 17:05:20 GMT
- Organization: Zippo
- Message-ID: <4ku0vd$lg3@clark.zippo.com>
- References: <4jmtlg$smm@baskerville.CS.Arizona.EDU>
- NNTP-Posting-Host: ddata116.dialdata.com.br
- X-Newsreader: Forte Free Agent 1.0.82
-
- kdb@CS.Arizona.EDU (Koen De Bosschere) wrote:
-
- >In order to prevent to write an iterator for every operation
- >I want to perform on a list, I tried to write a kind of
- >meta-iterator that would apply a particular method to
- >all listnodes. For some reason, I cannot execute a
- >function variable on an object. Does anyone has an
- >idea how I can solve this problem?
-
- >Thanks.
-
- >-- Koen De Bosschere
-
- >Test program:
-
- >#include <iostream.h>
-
- >class testclass {
- >public:
- > testclass(long d) { data = d; }
- > ~testclass() {}
- > void print() { cout << data << endl; }
- > testclass *getnext() { return next; }
- >private:
- > long data;
- > testclass *next;
- >};
-
- >// The next function applies the function "f" to all
- >// the nodes starting at listnode "root".
-
- >void iterate(testclass *root, void (testclass::*f)())
- >{
- > testclass *p = root;
- > while (p) {
- > p->f(); // <--- here the compiler generates an error (see below)
- > p = p->getnext();
- > }
- >}
-
- Borland C++ says you cannot take the address of a member function
- because it is really different from other kinds of function since it
- receives the "this" hidden argument.
-
- But in fact that is not the reason you get that bug. You cannot use
- p->f() without f being an explicity declared member function of your
- testclass.
-
- This problem is not trivial. Maybe templates can help you. Check it
- out.
-
- Cheers,
- Rodrigo Braz
-
-
-